<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/23 0023
 * Time: 21:52
 */

namespace app\Lib\Model\LotteryQualification;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\Present\Present;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\PresentRecord;

class RecordLottery implements \app\Lib\InterfaceIn\LotteryQualification\RecordLottery
{

    private $mobilePhone;
    /** @var UserHandle $user */
    private $user;
    /** @var Present $present */
    private $present;

    /**
     * @param mixed $mobilePhone
     */
    public function setMobilePhone($mobilePhone)
    {
        $this->mobilePhone = $mobilePhone;
    }

    /**
     * @param mixed $user
     */
    public function setUser(UserHandle $user)
    {
        $this->user = $user;
    }

    /**
     * @param mixed $present
     */
    public function setPresent(Present $present)
    {
        $this->present = $present;
    }

    /**
     * 执行代码
     * @return bool
     * @throws BlException
     */
    public function run()
    {
        $uid = 0;
        if(!empty($this->user)){
            $uid = $this->user->getUid();
        }
        $model = new PresentRecord();
        $data = [
            $model->_uid=>$uid,
            $model->_create_time=>time(),
            $model->_is_get=>0,
            $model->_mobile_phone=>$this->mobilePhone,
            $model->_present_id=>$this->present->getPresentId()
        ];
        $result = $model->insert($data);
        if(!$result){
            throw new BlException('添加抽奖记录失败');
        }
        return true;
    }


}